Imports Cdm.DataCore.Entities
Imports Cdm.DataCore.Budget
Imports System.Collections.Generic

Partial Class DashboardControls_ParticipantSummary
    Inherits System.Web.UI.UserControl
    Implements IDashBoard
    Dim titleLabelString As String = ""
    Dim pageSizeValue As Integer = 0

    Public Sub BindGrid() Implements IDashBoard.BindGrid
        'Declare DataCore objects to get participant, FMSA, and supportbroker data
        Dim participant As New Participant
        Dim FMSA As New FMSA
        Dim supportBroker As New SupportBroker

        'Gets current participant
        participant = EntityController.GetParticipant

        'Gets status, support broker ID, and FMSA Id from participant object
        Dim status As String = participant.Status
        Dim supportBrokerId As String = participant.SupportBrokerId
        Dim fmsaId As String = participant.FMSAId
        Me.FiscalAgencyDetailsButton.CommandArgument = fmsaId.ToString()
        Dim currStatus As List(Of Cdm.DataCore.Custom.ParticipantHomePageStatus) = Cdm.DataCore.Custom.ParticipantHomePageStatus.ListByParticipantId(participant.ParticipantId)

        If (currStatus.Count > 0) Then
            If currStatus(0).SpendingPlanAmount > 0 Then
                Me.lblSpendPlanLimit.Text = String.Concat(currStatus(0).SpendingPlanAmount.ToString("C"), " ", _
                                                        currStatus(0).BudgetPeriodDescription)
                If currStatus(0).Payments > 0 Then
                    Me.lblExpenditures.Text = String.Concat(currStatus(0).Payments, " ", currStatus(0).ExpendituresStartDate.ToShortDateString(), _
                                                            "-", currStatus(0).ExpendituresEndDate.ToShortDateString())
                End If
            End If
        End If

        
        If supportBrokerId > 0 Then
            supportBroker = supportBroker.Retrieve(supportBrokerId)

            'Get CDMUser object from supportBroker entity
            Dim supportBrokerUser As New Cdm.DataCore.UserSecurity.CDMUser
            supportBrokerUser = supportBroker.User
            Dim supportBrokerName = supportBrokerUser.DisplayLastNameFirst
            SupportBrokerDetailsButton.CommandArgument = supportBroker.SupportBrokerId
            lblSupportBroker.Text = supportBrokerName
        Else
            lblSupportBroker.Text = "None"
            SupportBrokerDetailsButton.Visible = False
        End If


        If fmsaId > 0 Then
            'Set agency name and support broker name
            Dim agencyName As String = FMSA.AgencyName
            'Retrieve FMSA data and Support Broker data for the FMSA Id and support broker Id above
            FMSA = FMSA.Retrieve(fmsaId)
            lblFiscalAgency.Text = agencyName

        Else
            lblFiscalAgency.Text = "None"
            FiscalAgencyDetailsButton.Visible = False
        End If

        lblStatus.Text = status


        lnkSpendPlanView.Text = "View current spending plan"
        lnkExpenditures.Text = "View financial history"
        lnkSpendPlanView.NavigateUrl = "~/Budget/SpendingPlanView.aspx"
        lnkExpenditures.NavigateUrl = "~/Budget/ExpenditureReportview.aspx"
    End Sub

    Public Sub LoadControl1(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDashBoard.LoadControl

    End Sub

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return "1"
        End Get
        Set(ByVal value As Integer)
            value = 1
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            Return 5
        End Get
        Set(ByVal value As Integer)            
            pageSizeValue = value
        End Set
    End Property

    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            Return WebControls.SortDirection.Ascending
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            Return ""
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.TitleLabel IsNot Nothing Then
                Return TitleLabel.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If TitleLabel IsNot Nothing Then
                TitleLabel.Text = value
            End If
            titleLabelString = value
        End Set
    End Property
    Protected Sub Page_Init(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles Me.Init

        'AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged

    End Sub


    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        'If Me.IsPostBack Then
        'AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged
        'End If
        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.TitleLabel.Text = titleLabelString
        End If

        Me.PageSize = 5
        Me.PageNumber = 1
        Me.SortField = ""
        Me.SortDirection = WebControls.SortDirection.Ascending

        If Not Me.IsPostBack Then

            GridViewSettingController.SetGridViewValues(Me, 5, "CriticalLevel", WebControls.SortDirection.Ascending)
            'Me.FilterViewedDropDownList.SelectedValue = GridViewSettingController.GetSecondarySetting(Me, "0")
            BindGrid()
        End If


    End Sub

    Protected Sub FiscalAgencyDetailsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FiscalAgencyDetailsButton.Click
        Dim agencyDetailsButton As LinkButton = sender
        Dim fmsaId As Integer = Convert.ToInt32(agencyDetailsButton.CommandArgument)
        Dim currentFMSA As Cdm.DataCore.Entities.FMSA = Cdm.DataCore.Entities.FMSA.Retrieve(fmsaId)
        Me.DetailsHeaderLabel.Text = "Agency Details for " + currentFMSA.AgencyName
        Me.Address1Value.Text = currentFMSA.Address1
        Me.Address2Value.Text = currentFMSA.Address2
        Me.AgencyValueLabel.Text = currentFMSA.AgencyName
        Me.ModalPopupDetails.Show()
    End Sub

    Protected Sub SupportBrokerDetailsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SupportBrokerDetailsButton.Click
        Dim detailsButton As LinkButton = sender
        Dim supportBrokerId As Integer = detailsButton.CommandArgument
        Dim currentSB As Cdm.DataCore.Entities.SupportBroker = Cdm.DataCore.Entities.SupportBroker.Retrieve(supportBrokerId)
        Dim currentAgency As Cdm.DataCore.Entities.SupportBrokerAgency
        If currentSB.SupportBrokerAgencyId > 0 Then
            currentAgency = Cdm.DataCore.Entities.SupportBrokerAgency.Retrieve(currentSB.SupportBrokerAgencyId)
        End If

        Me.DetailsHeaderLabel.Text = "Support Broker Details for " + currentSB.FullName
        Me.Address1Value.Text = currentSB.StreetAddress1
        Me.Address2Value.Text = currentSB.StreetAddress2
        If currentAgency Is Nothing Then
            Me.AgencyValueLabel.Text = "None"
        Else
            Me.AgencyValueLabel.Text = currentAgency.AgencyName
        End If
        Me.ModalPopupDetails.Show()

    End Sub
End Class
